/*
 * bomb.c
 *
 *  Created on: 26 f�vr. 2013
 *      Author: Yo
 */

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stddef.h>
#include "SDL.h"
#include "SDL_image.h"

#include "../include/game.h"
#include "../include/misc.h"
#include "../include/bomb.h"
#include "../include/liste.h"
#include "../include/map.h"





struct s_bomb {

	int x, y;
	SDL_Surface * directed_img[4];
	int init_time;
	int bomb_range;
	enum bomb_state current_state;
};

t_bomb new_bomb(t_player player, int init_time) {
	t_bomb bomb = malloc(sizeof (struct s_bomb));
	bomb->init_time = init_time;
	bomb->x=player_get_x(player);
	bomb->y=player_get_y(player);
	bomb->bomb_range=player_get_bomb_range(player);

	bomb->directed_img[BEGIN]=load_image("sprite/bomb4.png");
	bomb->directed_img[INT1]=load_image("sprite/bomb3.png");
	bomb->directed_img[INT2]=load_image("sprite/bomb2.png");
	bomb->directed_img[END]=load_image("sprite/bomb1.png");

	bomb->current_state=BEGIN;
	return bomb;
}

void display_bomb(t_bomb bomb, SDL_Surface *screen) {
	assert(bomb);
	assert(screen);
	assert(bomb->directed_img[bomb->current_state]);

	SDL_Rect place;
	place.x = bomb_get_x(bomb) * SIZE_BLOC;
	place.y = bomb_get_y(bomb) * SIZE_BLOC;


	SDL_BlitSurface(bomb->directed_img[bomb->current_state], NULL, screen,
			&place);
}

int bomb_get_x(t_bomb bomb) {
	assert(bomb != NULL);
	return bomb->x;
}

int bomb_get_y(t_bomb bomb) {
	assert(bomb);
	return bomb->y;
}

int bomb_get_init_time (t_bomb bomb) {
	assert(bomb);
	return bomb->init_time;
}



int update_bomb_status (t_bomb bomb,t_game game) {
	int lifetime=SDL_GetTicks()-bomb->init_time;
	if (lifetime>=3000 && lifetime<3500) {
		bomb->current_state=END;
		return 0;
	}
	else if (lifetime<3000 && lifetime>=2000) {
		//map_set_cell_type(map, bomb_get_x(l->bomb),bomb_get_y(l->bomb),CELL_BOMB_3);
		bomb->current_state=INT2;
		return 0;
	}
	else if (lifetime<2000 && lifetime>=1000) {
		bomb->current_state=INT1;
		//map_set_cell_type(map, bomb_get_x(l->bomb),bomb_get_y(l->bomb),CELL_BOMB_2);
		return 0;
	}
	else if (lifetime<1000 && lifetime>0){
		bomb->current_state=BEGIN;
		//map_set_cell_type(map, bomb_get_x(l->bomb),bomb_get_y(l->bomb),CELL_BOMB_1);
		return 0;
	}
	else if (lifetime>=3500 && lifetime<3530)
	{decrease_numb_set_bomb(game);
	return 1;
	}
	else{


		return 0;
	}

	return 1;
}

int bomb_get_bomb_range(t_bomb bomb) {
	assert (bomb);
	return bomb->bomb_range;
}

void new_bonus(t_map map,int x,int y) {
	int bonus_yn=rand()%10; //Y aura t-il un bonus ?
	if (bonus_yn <=7) { //Bonus
		int bonus_type=rand()%10; //Quel type de bonus ?
		if (bonus_type <=3){ //bonus bomb range
			int bomb_range=rand()%10;//Quel bonus ?
			if (bomb_range<=4){ // bomb range inc
				map_set_cell_type(map,x,y,BONUS_BOMB_RANGE_INC);
			}
			else {//bomb range dec
				map_set_cell_type(map,x,y,BONUS_BOMB_RANGE_DEC);
			}

		}
		else if (bonus_type>=4 && bonus_type<=7) {//Bonus bomb nb
			int bomb_nb=rand()%10;
			if (bomb_nb<=2){//bomb nb inc
				map_set_cell_type(map,x,y,BONUS_BOMB_NB_INC);
			}
			else { //bomb nb dec
				map_set_cell_type(map,x,y,BONUS_BOMB_NB_DEC);
			}
		}
		else if (bonus_type >=8) {//Bonus monstre

		}
	}
	else { //Pas de bonus
		map_set_cell_type(map,x,y,CELL_EMPTY);
	}
}

void case_explosion(t_map map,int x, int y,t_game game) {
	map_set_bonus(map);

	t_cell_type case_type=map_get_cell_type2(map,x,y);
	switch (case_type) {
	case BONUS_BOMB_NB_INC:
		map_set_cell_type(map,x,y,CELL_BONUS_BOMBINC);
		break;
	case BONUS_BOMB_NB_DEC:
		map_set_cell_type(map,x,y,CELL_BONUS_BOMBDEC);
		break;
	case BONUS_BOMB_RANGE_INC:
		map_set_cell_type(map,x,y,CELL_BONUS_RANGEINC);
		break;
	case BONUS_BOMB_RANGE_DEC:
		map_set_cell_type(map,x,y,CELL_BONUS_RANGEDEC);
		break;
	case BONUS_MONSTER:
		change_coormb(game,x,y);
		map_set_cell_type(map,x,y,CELL_EMPTY);
		break;
	case BONUS_EMPTY:
		map_set_cell_type(map,x,y,CELL_EMPTY);
		break;
	case BONUS_LIFE:
		map_set_cell_type(map,x,y,CELL_BONUS_LIFE);
		break;
	default:
		break;
	}


}




void bomb_explosion (t_bomb bomb,t_game game,SDL_Surface *screen ) {
	int bomb_x=bomb_get_x(bomb);
	int bomb_y=bomb_get_y(bomb);
	t_map map = level_get_curr_map(game_get_curr_level(game));
	int bomb_range=bomb_get_bomb_range(bomb);
	int i;


	int end_right=0;
	int end_left=0;
	int end_top=0;
	int end_bottom=0;

	for (i = 1 ; i <= bomb_range ; i++) {


		
		
		
		
if(map_is_inside(level_get_curr_map(game_get_curr_level(game)),bomb_x,bomb_y+i))
	{
		if (end_top == 0 ) {
      t_cell_type upper=map_get_cell_type(map,bomb_x,bomb_y+i);
			switch (upper & 15) { // type is encoded with 4 bits (the & 15 (1111)) is a mask to keep the four less significant bits)

			case CELL_GOAL:
				end_top=1;
				break;
			case CELL_SCENERY:
				end_top=1;
				break;
			case CELL_CASE:
				if(bomb_y+i<=11) {

					/*SDL_Rect place1;
						place1.x = bomb_x * SIZE_BLOC;
						place1.y = bomb_y+1 * SIZE_BLOC;
						display_bonus(map,screen,&place1,upper);*/
					//map_set_cell_type(map,bomb_x,bomb_y+1,CELL_BONUS_RANGEINC);
					//new_bonus(map,bomb_x,bomb_y+1);
					case_explosion(map,bomb_x,bomb_y+i,game);
					end_top=1;
				}
				break;
			case CELL_BONUS:
				end_top=1;
				break;
			case CELL_DOOR:
				end_top=1;
				break;
			case CELL_KEY:
				end_top=1;
				break;
			case CELL_BOMB:
				end_top=1;
				break;
			case CELL_EMPTY:
				break;
			case CELL_MONSTER:

				increase_game_destructionn(game);
				change_coorn(game,bomb_x,bomb_y+i);
				//map_set_cell_type(map,bomb_x,bomb_y+1,CELL_EMPTY);
				end_top=1;
				break;
			case CELL_PLAYER:
				player_dec_nb_life(game_get_player(game));
				end_top=1;
				break;
			}
		}
      }
      
      if(map_is_inside(level_get_curr_map(game_get_curr_level(game)),bomb_x,bomb_y-i))
	{

		if (end_bottom == 0) {
      t_cell_type lower=map_get_cell_type(map,bomb_x,bomb_y-i);
			switch (lower & 15) { // type is encoded with 4 bits (the & 15 (1111)) is a mask to keep the four less significant bits)
			case CELL_GOAL:
				end_bottom=1;
				break;
			case CELL_SCENERY:
				end_bottom=1;
				break;
			case CELL_CASE:
				if(bomb_y-i>=0){
					/*printf("%d",lower);
							SDL_Rect place2;
							place2.x = bomb_x * SIZE_BLOC;
							place2.y = bomb_y-1 * SIZE_BLOC;
							display_bonus(map,screen,&place2,lower);*/
					//map_set_cell_type(map,bomb_x,bomb_y-1,CELL_BONUS_RANGEINC);
					//new_bonus(map,bomb_x,bomb_y-1);
					case_explosion(map,bomb_x,bomb_y-i,game);
					end_bottom=1;
				}
				break;
			case CELL_BONUS:
				end_bottom=1;
				break;
			case CELL_KEY:
				end_bottom=1;
				break;
			case CELL_DOOR:
				end_bottom=1;
				break;
			case CELL_BOMB:
				end_bottom=1;
				break;
			case CELL_EMPTY:
				break;
			case CELL_MONSTER:
				increase_game_destructions(game);
				change_coors(game,bomb_x,bomb_y-i);
				//map_set_cell_type(map,bomb_x,bomb_y-1,CELL_EMPTY);
				end_bottom=1;
				break;
			case CELL_PLAYER:
				player_dec_nb_life(game_get_player(game));
				end_bottom=1;
				break;
			}
		}
      }
      
      if(map_is_inside(level_get_curr_map(game_get_curr_level(game)),bomb_x-i,bomb_y))
	{
		if (end_left == 0){
      t_cell_type left=map_get_cell_type(map,bomb_x-i,bomb_y);
			switch (left & 15) { // type is encoded with 4 bits (the & 15 (1111)) is a mask to keep the four less significant bits)
			case CELL_GOAL:
				end_left=1;
				break;
			case CELL_SCENERY:
				end_left=1;
				break;
			case CELL_CASE:
				if(bomb_x-i>=0){
					/*SDL_Rect place3;
								place3.x = bomb_x-1 * SIZE_BLOC;
								place3.y = bomb_y * SIZE_BLOC;
								display_bonus(map,screen,&place3,left);*/
					//map_set_cell_type(map,bomb_x-1,bomb_y,CELL_BONUS_RANGEINC);
					//new_bonus(map,bomb_x-1,bomb_y);
					case_explosion(map,bomb_x-i,bomb_y,game);
					end_left=1;
				}
				break;
			case CELL_BONUS:
				end_left=1;
				break;
			case CELL_KEY:
				end_left=1;
				break;
			case CELL_DOOR:
				end_left=1;
				break;
			case CELL_BOMB:
				end_left=1;
				break;
			case CELL_EMPTY:
				break;
			case CELL_MONSTER:

				increase_game_destructiono(game);
				change_cooro(game,bomb_x-i,bomb_y);
				//map_set_cell_type(map,bomb_x-1,bomb_y,CELL_EMPTY);
				end_left=1;
				break;
			case CELL_PLAYER:
				player_dec_nb_life(game_get_player(game));
				end_left=1;
				break;
			}
		}
      }
      
      if(map_is_inside(level_get_curr_map(game_get_curr_level(game)),bomb_x+i,bomb_y))
	{
		if (end_right == 0) {
      t_cell_type right=map_get_cell_type(map,bomb_x+i,bomb_y);
			switch (right & 15) { // type is encoded with 4 bits (the & 15 (1111)) is a mask to keep the four less significant bits)
			case CELL_GOAL:
				end_right=1;
				break;
			case CELL_SCENERY:
				end_right=1;
				break;
			case CELL_CASE:
				if(bomb_x+i<=11){
					//map_set_cell_type(map,bomb_x+1,bomb_y,CELL_BONUS_RANGEINC);
					//new_bonus(map,bomb_x+1,bomb_y);
					//								SDL_Rect place4;
					//								place4.x = bomb_x+1 * SIZE_BLOC;
					//								place4.y = bomb_y * SIZE_BLOC;
					//								display_bonus(map,screen,&place4,right);
					case_explosion(map,bomb_x+i,bomb_y,game);
					end_right=1;
				}
				break;
			case CELL_BONUS:
				end_right=1;
				break;
			case CELL_KEY:
				end_right=1;
				break;
			case CELL_DOOR:
				end_right=1;
				break;
			case CELL_BOMB:
				end_right=1;
				break;
			case CELL_EMPTY:
				break;
			case CELL_MONSTER:

				increase_game_destructione(game);
				change_coore(game,bomb_x+i,bomb_y);
				//map_set_cell_type(map,bomb_x+1,bomb_y,CELL_EMPTY);
				end_right=1;
				break;
			case CELL_PLAYER:
				player_dec_nb_life(game_get_player(game));
				end_right=1;
				break;
			}
		}
      }
		//if((player_get_x(aame_get_player(game) = x && player_get_y(game->player) = y+1) || (player_get_x(game->player) = x && player_get_y(game->player) = y-1) || (player_get_x(game->player) = x+1 && player_get_y(game->player) = y) || (player_get_x(game->player) = x-1 && player_get_y(game->player) = y)) {

		map_set_cell_type(map,bomb_get_x(bomb),bomb_get_y(bomb),CELL_EMPTY);



	}

}
